package com.ejuornal.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.Artical;
import com.ejuornal.bean.EditorCommit;
import com.ejuornal.bean.Message;
import com.ejuornal.bean.Page;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.User;
import com.ejuornal.dao.impl.MessageDaoImpl;
import com.ejuornal.service.ArticalService;
import com.ejuornal.service.MessageService;
import com.ejuornal.service.PeriodicalService;
import com.ejuornal.service.UserService;

@Component("messageService")
public class MessageServiceImpl implements MessageService {
	private MessageDaoImpl messageDao;

	public MessageDaoImpl getMessageDao() {
		return messageDao;
	}

	@Resource(name = "messageDao")
	public void setMessageDao(MessageDaoImpl messageDao) {
		this.messageDao = messageDao;
	}

	public void add(Message message) {
		String sql = "INSERT INTO t_message(title, content, sender, messageType, receiver, sendDate) VALUES( ?, ?, ?, ?, ?,now())";
		messageDao.add(
				sql,
				new Object[] { message.getTitle(), 
								message.getContent(),
								message.getSender(),
								"0", 
								message.getReceiver() });
	}
	public Message findById(int messageId) {
		Message message = messageDao.findById(messageId);
		
		return message;
	}

	public Page<Message> findByProperty(String prop, Object object) {
		return null;
	}
	
	public Page<Message> find(int current_record, String url, String hsql, Object[] objs) {
	    return messageDao.find(current_record, url, hsql, objs);
	}

    @Override
    public boolean findNew(String user_email) {
        return messageDao.findNew(user_email);
    }

	@Override
	public Message findById(long messageId) {
		return messageDao.findById(messageId);
	}

	@Override
	public boolean delete(Long id) {
		return messageDao.delete(id);
	}

	@Override
	public boolean updateIsRead(int msgId) {
		return messageDao.updateIsRead(msgId);
	}

	@Override
	public void sendMessage(PeriodicalService ps,ArticalService as,UserService us,MessageService ms,int articalId,String status) {
		Periodical periodical = ps.findPeriodicalByArticalId(articalId);
		Artical artical = as.findById(articalId);
		User user = us.getUser(articalId);
		Message message = new Message();
		message.setTitle("稿件通知");
		message.setReceiver(user.getEmail());	//收件人  的邮箱
		message.setSender(periodical.getName()); //期刊名字
		message.setMessageType("0");
		//  1  恭喜通过  2 退稿
		if(status.equals("1")){
			message.setContent("您的文章《 " + artical.getTitle() + "》通过审核，等待发表。");//  
		}else {
			message.setContent("您的文章《 " + artical.getTitle() + "》没有通过审核，再接再励。");//  
		}
				System.out.println(message);
		ms.add(message);
		
	}

	@Override
	public void sendMessage(User user, MessageService ms, String status) {
		// TODO Auto-generated method stub
		Message message = new Message();
		message.setTitle("稿件通知");
		message.setReceiver(user.getEmail());	//收件人  的邮箱
		message.setSender("ejuornal"); //期刊名字
		message.setMessageType("0");
		if(status.equals("1")){
			message.setContent("您已通过审核，可以创建期刊。");//  
		}else {
			message.setContent("您的公司主编审核不合格，请重新审核！");//  
		}
		ms.add(message);
	}

	@Override
	public void sendMessage(PeriodicalService ps, MessageService ms,UserService us,
			EditorCommit ec, String status) {
		// TODO Auto-generated method stub
		User user = us.findById(ec.getUserId());
		String name = ps.findById(ec.getPeriodicalId()).getName();
		Message message = new Message();
		message.setTitle("稿件通知");
		message.setReceiver(user.getEmail());	//收件人  的邮箱
		message.setSender(name); //期刊名字
		message.setMessageType("0");
		if(status.equals("1")){
			message.setContent("您已通过审核，成为" + name + "的编委。");//  
		}else {
			message.setContent("您没有通过审核，不能成为" + name + "的编委。");//  
		}
		ms.add(message);
		
	}

}
